﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Galaktika.BI.Silverlight.Controls
{
    public static class ContextMenuProvider
    {
        public static readonly DependencyProperty ContextMenuModeProperty =
            DependencyProperty.RegisterAttached("ContextMenuMode",
            typeof(ContextMenuSupportMode),
            typeof(ContextMenuProvider),
            null);

        public static ContextMenuSupportMode GetContextMenuMode(FrameworkElement fe)
        {
            return (ContextMenuSupportMode)fe.GetValue(ContextMenuModeProperty);
        }

        public static void SetContextMenuMode(FrameworkElement fe, ContextMenuSupportMode mode)
        {
            fe.SetValue(ContextMenuModeProperty, mode);
        }

        public static void AddContextMenu(
            this FrameworkElement fe, 
            Ranet.AgOlap.Controls.ContextMenu.CustomContextMenu menu,
            ContextMenuSupportMode mode)
        {
            SetContextMenuMode(fe, mode);
            if (mode == ContextMenuSupportMode.OnRightMouseButtonClick)
            {
                fe.MouseEnter += fe_MouseEnter;
                fe.MouseLeave += fe_MouseLeave;
            }
            if (mode == ContextMenuSupportMode.OnCtrlLeftMouseButtonClick)
            {
                fe.MouseLeftButtonUp += fe_MouseLeftButtonUp;
            }
        }

        static void fe_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
        }

        public static void AddContextMenu(
            this FrameworkElement fe, 
            Ranet.AgOlap.Controls.ContextMenu.CustomContextMenu menu)
        {
            AddContextMenu(fe, menu, ContextMenuSupportMode.Both);
        }

        public static void RemoveContextMenu(this FrameworkElement fe)
        {
        }

        static void fe_MouseLeave(object sender, MouseEventArgs e)
        {
            if (System.Windows.Browser.HtmlPage.IsEnabled)
            {
                HtmlPage.Document.DetachEvent("oncontextmenu", new EventHandler<HtmlEventArgs>(ContentMenu_EventHandler));
            }
        }

        static void fe_MouseEnter(object sender, MouseEventArgs e)
        {
            if (System.Windows.Browser.HtmlPage.IsEnabled)
            {
                HtmlPage.Document.AttachEvent("oncontextmenu", new EventHandler<HtmlEventArgs>(ContentMenu_EventHandler));
            }
        }

        static void ContentMenu_EventHandler(object sender, HtmlEventArgs e)
        {
            e.PreventDefault();
            e.StopPropagation();
            if (System.Windows.Browser.HtmlPage.IsEnabled)
            {
                //HtmlPage.Window.Alert(string.Format("X: {0}\r\nY: {1}", e.OffsetX, e.OffsetY));
            }
        }
    }

    [Flags]
    public enum ContextMenuSupportMode
    {
        OnRightMouseButtonClick,
        OnCtrlLeftMouseButtonClick,
        Both = OnCtrlLeftMouseButtonClick | OnRightMouseButtonClick
    }
}
